package de.tabuma.pm.externalcontent.controller;

import de.tabuma.lib.tabumacontroller;
import de.tabuma.pm.*;
import de.tabuma.pm.projectnavigation.entity.*;
import de.tabuma.pm.projectnavigation.logik.*;
import de.tabuma.pm.externalcontent.entity.*;
import de.tabuma.pm.externalcontent.logik.*;
import java.io.PrintWriter;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;

/**
 *
 * @author alefen
 */
public class controller extends tabumacontroller {

    private static final Logger log = Logger.getLogger(controller.class.getName());

    public controller(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {

        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        String projectNavigationId = request.getParameter(constants.ProjectNavigationId);
        int projectNavigationIdInt = Integer.valueOf(projectNavigationId).intValue();
        ExternalContent ec = ExternalContentFacade.getWithNavigationId(projectNavigationIdInt);

        if(request.getParameter(constants.externalContentUpdate)!=null) {
            String externalProjectId = request.getParameter(constants.externalProjectId);
            int externalProjectIdInt = Integer.valueOf(externalProjectId).intValue();
            String externalContentUrl = request.getParameter(constants.externalContentUrl);
            if(ec==null) {
                ec = new ExternalContent();
                ec.setNavigationId(projectNavigationIdInt);
                ec.setExternProjectId(externalProjectIdInt);
                ec.setUrl(externalContentUrl);
                ExternalContentFacade.create(ec, user.getEmail());
            }
            else {
                ec.setNavigationId(projectNavigationIdInt);
                ec.setExternProjectId(externalProjectIdInt);
                ec.setUrl(externalContentUrl);
                ExternalContentFacade.update(ec, user.getEmail());
            }
            ProjectNavigation pn = ProjectNavigationFacade.get(projectNavigationIdInt);
            pn.setNavMode(constants.ProjectNavigationExternalContent);
            ProjectNavigationFacade.update(pn, user.getEmail());
        }
        request.setAttribute(constants.externalContent, ec);
    }
}
